package com.java1234.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java1234.entity.Config;
import com.java1234.entity.PageBean;
import com.java1234.entity.R;
import com.java1234.service.ConfigService;
import com.java1234.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 配置文件 前端控制器
 * </p>
 *
 * @author zhai
 * @since 2025-05-09
 */
@RestController
@RequestMapping("/sys/config")
public class ConfigController {
    @Autowired
    private ConfigService service;

    @PostMapping("/save")
//    @PreAuthorize("hasAuthority('system:config:save')")
    public R save(@RequestBody List<Config> configs) {
        boolean result = true;
        for (Config config : configs) {
            boolean b = service.updateById(config);
            if (!b) result = false;
        }
        if(result){
            return R.ok();
        }else{
            return R.error("保存失败");
        }
    }

    @PostMapping("/list")
//    @PreAuthorize("hasAuthority('system:config:list')")
    public R list(@RequestBody PageBean pageBean){
        String query = pageBean.getQuery() != null ? pageBean.getQuery().trim() : "";
        Page<Config> pageResult = service.page(
                new Page<>(pageBean.getPageNum(), pageBean.getPageSize()),
                new QueryWrapper<Config>()
                        .like(StringUtil.isNotEmpty(query), "config", query)
                        .orderByDesc("id") // 改为查询config字段而不是username
        );
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("list",pageResult.getRecords());
        resultMap.put("total",pageResult.getTotal());
        return R.ok(resultMap);
    }

}
